package diploma.kisel.modelas.service;

import java.util.ArrayList;
import java.util.List;

import diploma.kisel.modelas.domain.Computer;
import diploma.kisel.modelas.domain.ComputerType;
import diploma.kisel.modelas.repository.ComputerRepositoryImpl;
import diploma.kisel.modelas.repository.api.ComputerRepository;
import diploma.kisel.modelas.service.api.ComputerService;

public class ComputerServiceImpl implements ComputerService {

	private ComputerRepository computerRepository = new ComputerRepositoryImpl();

	@Override
	public List<Computer> findAll() {
		return computerRepository.findAll();
	}

	@Override
	public List<Computer> findByPrice(int minPrice, int maxPrice) {
		return computerRepository.findByPrice(minPrice, maxPrice);
	}

	@Override
	public Computer save(Computer computer) {
		if (computer.getId() != null)
			return computerRepository.update(computer);
		else
			return computerRepository.save(computer);
	}

	@Override
	public void remove(Computer computer) {
		if (computer.getId() != null)
			computerRepository.remove(computer);
	}

	@Override
	public Computer findById(int id) {
		return computerRepository.findById(id);
	}

	@Override
	public void remove(int id) {
		Computer computer = computerRepository.findById(id);
		computerRepository.remove(computer);
	}

	@Override
	public List<Computer> findByType(ComputerType type) {
		return computerRepository.findByType(type);
	}

	@Override
	public List<Computer> findByBrand(List<Computer> computers, String brand) {
		List<Computer> result = new ArrayList<Computer>();
		for (Computer computer: computers)
			if (computer.getBrand().startsWith(brand))
				result.add(computer);
		return result;
	}

	@Override
	public List<Computer> findByPriceAndBrand(int minPrice, int maxPrice,
			ComputerType type) {
		return computerRepository.findByPriceAndType(minPrice, maxPrice, type);
	}

}
